<!--
/* 设置左侧菜单字体的样式 */

var log, className = "dark";
var chapterIdValue;
function addHoverDom(treeId, treeNode) {
    var sObj = $("#" + treeNode.tId + "_span");
    if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0)
    {
        return;
    }
    var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
        + "' title='添加子章节' onfocus='this.blur();'></span>";
    sObj.after(addStr);
    var btn = $("#addBtn_"+treeNode.tId);
    if (btn)
    {
        btn.bind("click", function(){
            var zTree = $.fn.zTree.getZTreeObj("leftBookChapterEditMenu");

            $.get("/details/add",{bookId:document.getElementById('formInputBookId').value,parentId:treeNode.id,title:"未命名"},function (resultJSONObject){
                if (resultJSONObject.code === 200) {
                    var chapterObj = resultJSONObject.msg;
                    zTree.addNodes(treeNode, {id:chapterObj.id, pId:chapterObj.parentId, name:chapterObj.title});

                }else{}
            },"json");
            return false;
        });
    }
};
function removeHoverDom(treeId, treeNode) {
    $("#addBtn_"+treeNode.tId).unbind().remove();
};
function beforeDrag(treeId, treeNodes) {
    return false;
}
function beforeEditName(treeId, treeNode) {
    className = (className === "dark" ? "":"dark");
    var zTree = $.fn.zTree.getZTreeObj("leftBookChapterEditMenu");
    zTree.selectNode(treeNode);
    setTimeout(function() {
        // if (confirm("进入节点 -- " + treeNode.name + " 的编辑状态吗？")) {
        //     setTimeout(function() {
        //         zTree.editName(treeNode);
        //     }, 0);
        // }
        zTree.editName(treeNode);
    }, 0);
    return false;
}
function beforeRemove(treeId, treeNode) {
    className = (className === "dark" ? "":"dark");
    var zTree = $.fn.zTree.getZTreeObj("leftBookChapterEditMenu");
    zTree.selectNode(treeNode);
    var boole = confirm("确认删除 节点 -- " + treeNode.name + " 吗？");
    if(boole)
    {
        $.get('/system/ebook/chapter/delete', {"chapterIds": treeNode.id}, function (r) {
            if (r.code === 200) {
                boole = true;
            } else {
                boole = false;
            }
        });
    }
    return boole;
}
function beforeRename(treeId, treeNode, newName, isCancel) {
    className = (className === "dark" ? "":"dark");
    if (newName.length == 0) {
        setTimeout(function() {
            var zTree = $.fn.zTree.getZTreeObj("leftBookChapterEditMenu");
            zTree.cancelEditName();
            alert("章节名称不能为空.");
        }, 0);
        return false;
    }
    var parent= treeNode.pId;
    if(parent==null)
    {
        parent=0;
    }
    $.get("/system/ebook/chapter/update", {id:treeNode.id,parentId:parent,bookId:document.getElementById('formInputBookId').value,title:newName}, function (r) {
        if (r.code === 200) {
            return true;
        } else return false;
    });
    return true;
}

function addChapterMenu(e){
    var zTree = $.fn.zTree.getZTreeObj("leftBookChapterEditMenu");
    $.get("/system/ebook/chapter/add",{bookId:document.getElementById('formInputBookId').value,parentId:0,title:"未命名"},function (resultJSONObject){
        if (resultJSONObject.code === 200) {
            var chapterObj = resultJSONObject.msg;
            zTree.addNodes(null, {id:chapterObj.id, pId:chapterObj.parentId, name:chapterObj.title});

        }else{}
    },"json");
};
function saveChapterContent(e){


    $.post("/system/ebook/chapter/updateChapterContent", {id: document.getElementById('formInputContentId').value,content:chapterContentCKEditor.getData()}, function (r) {
        if (r.code === 200) {

        } else{};
    });
}
function ajaxDataFilter(treeId, parentNode, responseData) {
    return responseData;
};

var setting = {
    view: {
        addHoverDom: addHoverDom,
        removeHoverDom: removeHoverDom,
        showIcon: false,
        selectedMulti: false,
        // fontCss: setLeftBookChapterEditMenuFontCss
    },
    edit: {
        enable: true,
        editNameSelectAll: true,
        showRemoveBtn: true,
        removeTitle:"删除",
        showRenameBtn: true,
        renameTitle:"重命名"
    },
    data: {
        key: {
            title:"t",
        },
        simpleData: {
            enable: true
        }
    },
    async:{
        enable: true,
        dataType: "text",
        contentType: "application/json",
        type: "post",
        headers:{},
        url: "/details/listData?bookId="+ document.getElementById('formInputBookId').value,
        autoParam: ["id"], /*传递父节点*/
        dataFilter: ajaxDataFilter,
        xhrFields:{},
    },
    callback: {
        beforeDrag: beforeDrag,
        beforeEditName: beforeEditName,
        beforeRemove: beforeRemove,
        beforeRename: beforeRename,
        onClick:leftBookChapterMenuOnMouseDown,
    }
};

var log, className = {down:"dark", up:"dark", right:"dark"};

function leftBookChapterMenuOnMouseDown(event, treeId, treeNode) {
    getBookChapterContent(treeNode.id);
}
$(document).ready(function(){
    $.fn.zTree.init($("#leftBookChapterEditMenu"), setting);
    $("#addChapterMenu").bind("click", {isParent:true}, addChapterMenu);
    $("#saveChapterContent").bind("click", {isParent:true}, saveChapterContent);
    creatCKEditor();
});
//-->